package org.mspring.mlog.service;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.mspring.mlog.entity.Skin;
import org.mspring.mlog.entity.constants.Keys;
import org.mspring.mlog.entity.constants.OptionKeys;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.utils.PropertiesUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.spring.view.freemarker.ExtendsFreeMarkerViewResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

/**
 * @author Gao Youbo
 * @since 2012-7-24
 */
@Service
public class SkinService {

    private static final Logger log = LoggerFactory.getLogger(SkinService.class);

    @Autowired
    private ConfigService configService;
    @Autowired
    private ResourceLoader resourceLoader;

    public String getCurrentSkinFolder() {
        String skin = configService.getConfig(OptionKeys.CURRENT_SKIN);
        if (StringUtils.isBlank(skin)) {
            skin = "default";
        }
        return skin;
    }

    public List<Skin> scrnSkin() {
        // 存放主题的目录, 默认为/skins
        final String skinfolder = ExtendsFreeMarkerViewResolver.getSkinfolder();
        final Resource resource = resourceLoader.getResource(skinfolder);
        List<Skin> list = new ArrayList<>();
        try {
            File skinfolder_file = resource.getFile();
            File[] skinfiles = skinfolder_file.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {

                    if (!pathname.isDirectory()) {
                        return false;
                    }
                    String skin_properties_filename = pathname.getPath() + File.separator + Keys.SKIN_PROPERTIES_NAME;
                    File skin_properties_file = new File(skin_properties_filename);
                    if (!skin_properties_file.exists() || skin_properties_file.isDirectory()) {
                        return false;
                    }
                    return true;
                }
            });

            for (File skinfile : skinfiles) {
                String skin_properties_filename = skinfile.getPath() + File.separator + Keys.SKIN_PROPERTIES_NAME;
                File skin_properties_file = new File(skin_properties_filename);
                Map<String, String> properties = PropertiesUtils.getPropertyMap(skin_properties_file);
                Skin skin = new Skin();
                skin.setFolder(skinfile.getName());
                skin.setName(properties.get("name"));
                skin.setAuthor(properties.get("author"));
                skin.setUrl(properties.get("url"));
                skin.setEmail(properties.get("email"));
                skin.setDescription(properties.get("description"));
                list.add(skin);
            }
            return list;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return list;
    }
}
